﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace server.SignalrServer
{
    public class InstantMessage : Hub<IInstantMessage>
    {
        /// <summary>
        /// 有连接进入执行
        /// </summary>
        /// <returns></returns>
        public override Task OnConnectedAsync()
        {
            SendMessageAll("连接成功").Wait();
            return base.OnConnectedAsync();
        }
        /// <summary>
        /// 连接断开执行
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public override Task OnDisconnectedAsync(Exception exception)
        {
            SendMessageAll("断开连接").Wait();
            return base.OnDisconnectedAsync(exception);
        }
        /// <summary>
        /// 给所有人发消息
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <returns></returns>
        public async Task SendMessageAll(string message)
        {
            await Clients.All.ReceiveMessage(message);
        }
    }
}
